import java.util.Scanner;

public class Clinet {
    public static void main(String[] args) {
        Scanner sc=new Scanner(System.in);
        FoodManager fm=new FoodManager();
        while (true){
            System.out.println("1.可添加新食品.");
            System.out.println("2.根据食品名称，查询食品信息");
            System.out.println("3.查询所有商品信息");
            System.out.println("4.根据食品名称，修改食品信息");
            System.out.println("5.计算所有食品总价");
            System.out.println("6.统计所有食品中最贵的商品信息");
            System.out.println("7.按照食品的价格升序排序输出这些食品的信息");
            System.out.println("8.退出");
            int select=sc.nextInt();
            switch (select){
                case 1:
                    addFood(sc, fm);//调用添加方法
                    break;
                case 2:
                    FindByName(sc, fm);//调用查询方法
                    break;
                case 3:
                    printFood(fm.findAll());//查询所有信息
                    break;
                case 4:
                    updateByName(sc, fm);//通过名字修改信息
                    break;
                case 5:
                    System.out.println("总价为："+fm.countPrice());//计算总价
                    break;
                case 6:
                    printFood(fm.maxFood());//最贵的食品信息
                    break;
                case 7:
                    printFood(fm.sortFood());//价格排序
                    break;
                case 8:
                    System.out.println("再见");
                    return;
                default:
                    System.out.println("输入有误");
            }
        }
    }

    private static void updateByName(Scanner sc, FoodManager fm) {
        System.out.println("请输入需要修改的食品名称");
        String name= sc.next();
        if(fm.updateByname(name)){
            System.out.println("请修改价格");
            double price= sc.nextDouble();
            System.out.println("请修改库存");
            int in= sc.nextInt();
            boolean update = fm.updateByname(name,price,in);
            System.out.println (update?"修改成功":"修改失败");
        }else{
            System.out.println("该食品不存在");
        }
    }

    private static void FindByName(Scanner sc, FoodManager fm) {
        System.out.println("请输入你需要查找的食品名");
        String name= sc.next();
        printFood(fm.findByName(name));
    }

    private static void addFood(Scanner sc, FoodManager fm) {
        System.out.println("请输入产品名");
        String name= sc.next();
        System.out.println("请输入价格");
        double price= sc.nextDouble();
        System.out.println("请输入数量");
        int inventory= sc.nextInt();
        Food food=new Food(name,price,inventory);
        boolean ad= fm.add(food);
        System.out.println(ad?"添加成功":"添加失败");
    }

    // 打印输出的方法
    private static void printFood(Food[] foods) {
        System.out.println("---------------------------------------------");
        System.out.println("|名称\t\t\t|价格\t\t\t|库存\t\t|");
        for (int i = 0; i < foods.length; i++) {//循环遍历数组
            Food f = foods[i];
            String str = String.format("|%s\t\t\t|%.2f\t\t\t|%d\t\t\t|",f.getName(),f.getPrice(),f.getInventory());//获取数组信息
            System.out.println(str);//输出获取的信息
        }
        System.out.println("---------------------------------------------");
    }
}
